package com.gotkicry.foodmenu;

import android.os.Bundle;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.slidingpanelayout.widget.SlidingPaneLayout;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final String CategoryURL = "https://apis.juhe.cn/cook/category?key=4c418590b697b3860fd06132a2cdfe2f";
    private ExpandableListView mListMenu;
    private LinearLayout mFragment;
    private List<String> listGroup;
    private List<List<Map>> listChild;
    private RequestQueue requestQueue;
    private MyExpandAdapter adapter;
    private SlidingPaneLayout slidingPaneLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initData();
        replaceFragment();
    }

    private void initData() {
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, CategoryURL, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                JSONArray jsonArray = jsonObject.optJSONArray("result");
                for (int i = 0; i < jsonArray.length();i++){
                    try {
                        JSONObject jsb = jsonArray.getJSONObject(i);
                        listGroup.add(jsb.optString("name"));
                        JSONArray jsonArray1 = jsb.getJSONArray("list");
                        List<Map> list = new ArrayList<>();
                        for(int o = 0 ; o < jsonArray1.length(); o ++){
                            Map<String,String> map = new HashMap<>();
                            map.put("id",jsonArray1.getJSONObject(o).optString("id"));
                            map.put("name",jsonArray1.getJSONObject(o).optString("name"));
                            map.put("parentId",jsonArray1.getJSONObject(o).optString("parentId"));
                            list.add(map);
                        }
                        listChild.add(list);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
                Log.d(TAG, "onResponse: "+listChild.size()+"  "+listGroup.size());
                adapter = new MyExpandAdapter(listGroup,listChild);
                mListMenu.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(MainActivity.this, "网络错误或API次数已用完", Toast.LENGTH_SHORT).show();
            }
        });
        requestQueue.add(request);
    }

    private void initView() {
        mListMenu =  findViewById(R.id.list_menu);
        mFragment =  findViewById(R.id.fragment);
        slidingPaneLayout = findViewById(R.id.slid_layout);
        listChild = new ArrayList<>();
        listGroup = new ArrayList<>();
        requestQueue =  Volley.newRequestQueue(this);
    }


    class MyExpandAdapter extends BaseExpandableListAdapter{
        List<String> listGroup;
        List<List<Map>> listChild;

        public MyExpandAdapter(List<String> listGroup, List<List<Map>> listChild) {
            this.listGroup = listGroup;
            this.listChild = listChild;
        }

        @Override
        public int getGroupCount() {
            return listGroup.size();
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return listChild.get(groupPosition).size();
        }

        @Override
        public Object getGroup(int groupPosition) {
            return listGroup.get(groupPosition);
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return listChild.get(groupPosition).get(childPosition);
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_list_menu,parent,false);
            TextView textView = convertView.findViewById(R.id.text_menu_item);
            textView.setText(listGroup.get(groupPosition));
            return convertView;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            convertView = LayoutInflater.from(MainActivity.this).inflate(R.layout.item_list_menu,parent,false);
            final TextView textView = convertView.findViewById(R.id.text_menu_item);
            final Map<String,String> map = listChild.get(groupPosition).get(childPosition);
            textView.setText(map.get("name"));
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    replaceFragment(Integer.parseInt(map.get("id")));
                    slidingPaneLayout.closePane();
                }
            });
            return convertView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }
    void replaceFragment(){
        Bundle bundle = new Bundle();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        ShowFragment showFragment = new ShowFragment();
        showFragment.setArguments(bundle);
        transaction.replace(R.id.fragment,showFragment);
        transaction.commit();
    }
    void replaceFragment(int id){
        Bundle bundle = new Bundle();
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        ShowFragment showFragment = new ShowFragment();
        showFragment.setArguments(bundle);
        transaction.replace(R.id.fragment,showFragment);
        transaction.commit();
    }
}
